package com.spring.framework.config.autowired.model;

import org.springframework.stereotype.Component;

import javax.annotation.Nullable;

/**
 * 在Spring 5.0之后，我们还可以使用Spring提供的@Nullable注解或者JSR-305中的@Nullable注解标注到参数字段上，实现和Optional同样的“有就注入，没有就不注入”的功能。并且，@Nullable同样会抵消无论是默认的还是手动指定的required=true带来的强制依赖注入非null检查。
 *
 * @author xuweizhi
 * @since 2020/12/15 11:23
 */
@Component
public class AutowiredConstructorNullableDemo {

	private NullableDemoA nullableDemoA;

	private NullableDemoB nullableDemoB;


	public AutowiredConstructorNullableDemo(@Nullable NullableDemoA nullableDemoA,
											@Nullable NullableDemoB nullableDemoB) {
		System.out.println("调用两参数构造器");
		this.nullableDemoA = nullableDemoA;
		this.nullableDemoB = nullableDemoB;
	}

	@Component("nullableDemoA")
	public static class NullableDemoA {
		public NullableDemoA() {
			System.out.println("NullableDemoA：" + this);
		}
	}

	//@Component("nullableDemoB")
	public static class NullableDemoB extends NullableDemoA {
		public NullableDemoB() {
			System.out.println("NullableDemoB：" + this);
		}
	}

	@Override
	public String toString() {
		return "AutowiredConstructorNullableDemo{" +
				"nullableDemoA=" + nullableDemoA +
				", nullableDemoB=" + nullableDemoB +
				'}';
	}
}

